<script setup>
import BackLink from "@/components/BackLink/index.vue"
import {useRoute, useRouter} from "vue-router";
import {auditRecordApi, detailsAuditApi} from "@/api/school.js";
import ImagePreview from "@/components/ImagePreview/index.vue";
import {checkRole} from "@/utils/permission.js";
import {formatTypeName} from "../../../../utils/index.js";
import {isExternal} from "@/utils/validate.js";
const router = useRouter()
const route = useRoute()
const detailsInfo = ref({})

const {proxy} = getCurrentInstance();


const state = reactive({
  isApproval:false,
  showVideo:false,
})


const handleBack = () => {
  router.go(-1)
}

const getDetails = async () => {
  const res = await detailsAuditApi({id: route.query?.id})
  if(res.code == 200){
    detailsInfo.value = res.data
  }
}

const handleAudit = async (status) => {
  const res = await auditRecordApi({id: route.query?.id, auditStatus: status})
  if(res.code == 200){
    proxy.$message.success('操作成功')
    handleBack()
  }
}

const handleShowVideo = () => {
  state.showVideo = true
}

function getRealSrc(src){
  if (!src) {
    return;
  }
  let real_src = src.split(",")[0];
  if (isExternal(real_src)) {
    return real_src;
  }
  return import.meta.env.VITE_APP_BASE_API + real_src;
}

const handleShowDocument = () => {
  const filePath = getRealSrc(detailsInfo.value.auditFile)
  window.open(filePath)
}
onMounted(() => {
  if(route.query?.id){
    state.isApproval = true
    getDetails()
  }
})
</script>

<template>
  <div class="outBox">
    <el-card>
      <BackLink/>
      <div class="form-box">
        <div class="form-line">
          <div class="form-item">
            <div class="form-name">编号</div>
            <div class="form-cont">
              <div class="form-text">{{detailsInfo.auditCode}}</div>
            </div>
          </div>
          <div class="form-item">
            <div class="form-name">申请人</div>
            <div class="form-cont">
              <div class="form-text">{{detailsInfo.auditStu}}</div>
            </div>
          </div>
          <div class="form-item">
            <div class="form-name">类别</div>
            <div class="form-cont">
              <div class="form-text">{{formatTypeName(detailsInfo.auditType)}}</div>
            </div>
          </div>
        </div>
        <div class="form-line">
          <div class="form-item">
            <div class="form-name">证明材料</div>
            <div class="form-cont">
              <ImagePreview
                  v-if="detailsInfo.auditFileType === 'image'"
                  :width="50"
                  :src="detailsInfo.auditFile"
              />
              <el-link style="margin-left: 10px" type="primary" @click="handleShowVideo()" v-if="detailsInfo.auditFileType === 'video'">
                查看视频内容
              </el-link>

              <el-link style="margin-left: 10px" type="primary" @click="handleShowDocument()" v-if="detailsInfo.auditFileType === 'document'">
                查看文档内容
              </el-link>

              <div class="form-text" v-if="detailsInfo.auditFileType === 'document'">

              </div>
            </div>
          </div>
          <div class="form-item">
            <div class="form-name">审批人</div>
            <div class="form-cont">
              <div class="form-text">{{detailsInfo.auditUser}}</div>
            </div>
          </div>
          <div class="form-item">
            <div class="form-name">申请状态</div>
            <div class="form-cont">
              <div class="form-text" v-if="detailsInfo.auditStatus === 1">待审核</div>
              <div class="form-text" style="color: #1ab394"  v-if="detailsInfo.auditStatus === 2">审核通过</div>
              <div class="form-text" style="color: indianred" v-if="detailsInfo.auditStatus === 3">审核拒绝</div>
            </div>
          </div>
        </div>
        <div class="form-table">
          <div class="form-item">
            <div class="form-name">申请内容</div>
          </div>
          <div class="form-textArea">
            <p>{{detailsInfo.auditTitle}}</p>
            <p>{{detailsInfo.auditCont}}</p>
          </div>
        </div>
        <div class="form-table" style="margin-top: 20px" v-if="!state.isApproval">
          <div class="form-item">
            <div class="form-name">驳回原因</div>
          </div>
          <div class="form-textArea">
            <p>需要证明图片</p>
          </div>
        </div>
        <div class="center-btns">
          <el-button  @click="handleAudit(2)" type="primary" v-if="state.isApproval && !checkRole(['student']) ">通过</el-button>
          <el-button  @click="handleAudit(3)" type="primary" v-if="state.isApproval && !checkRole(['student']) ">驳回</el-button>
          <el-button  @click="handleBack" v-if="!state.isApproval">返回</el-button>
          <el-button  @click="handleBack" type="primary" v-if="!state.isApproval">提交</el-button>
        </div>
      </div>
    </el-card>

    <el-dialog
        v-model="state.showVideo"
        title="视频播放"
        width="1000"
        align-center
    >

      <video width="100%" height="500" controls>
        <source :src="getRealSrc(detailsInfo.auditFile)" type="video/mp4">
      </video>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="state.showVideo = false">关闭</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<style scoped lang="scss">
.form-text{
  color: var(--el-input-text-color,var(--el-text-color-regular));
  font-size: 14px;
  text-indent: 10px;
}
.text-items{
  padding: 20px;
  display: grid;
  justify-content: space-between;
  grid-template-columns: repeat(2, 550px);
  .text-item{
    margin-bottom: 20px;
  }
  .tit{
    font-size: 22px;
    margin-bottom: 5px;
  }
  .bottom{
    display: flex;
    justify-content: space-between;
    .time{
      color: #827e7d;
    }
  }
}
.form-table{
  border: 1px solid #e5e5e5;
  min-height:100px;
  table{
    margin-top: 20px;
  }
}
.center-btns{
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
.form-textArea{
  border: 1px solid #e5e5e5;
  min-height:100px;
  padding:0 20px;
  :deep(.el-textarea){
    --el-input-border-color:#fff !important;
    --el-input-focus-border:#fff !important;
    --el-input-hover-border-color:#fff !important;
    --el-input-focus-border-color:#fff !important;

  }
}
.form-box{
  .form-name{
    background: #f1f7ff;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100px;
    font-weight: bold;
    border-right: 1px solid #e5e5e5;
  }
  .form-line{
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    border: 1px solid #e5e5e5;
    margin-bottom: 20px;
    .form-item{
      display: flex;
      align-items: center;

      .form-cont{
        width: calc(100% - 100px);
        :deep(.el-input){
          --el-input-border-color:#fff !important;
          --el-input-focus-border:#fff !important;
          --el-input-focus-border-color:#fff !important;
        }
      }
    }
  }
}
</style>
